//package com.aotain.nyx.flink;
//
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//import org.apache.commons.codec.binary.Base64;
//import org.apache.flink.api.common.functions.MapFunction;
//import org.apache.flink.api.java.tuple.Tuple2;
//
//import com.aotain.apollo.IPDatabase;
//import com.aotain.apollo.IPSearcher;
//import com.aotain.common.CommonFunction;
//
//public class SourceFmtStat implements MapFunction<String,Tuple2<String,String>>{
//
//	/** 
//	* @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
//	*/ 
//	private static final long serialVersionUID = -6796416224788393651L;
//
//	@Override
//	public Tuple2<String, String> map(String logString) throws Exception {
//		
//		String refer = "";
//		String dip = "";
//		String sip = "";
//		String userAgent = "";
//		String cookie = "";
//		String dipL = "";
//		String sipL = "";
//		String dport = "";
//		String sport = "";
//		String domainname = "";
//		String url = "";
//		String type = "";
//		String accesstime = "";
//		String idcno = "";
//		String post = "0";	
//		String dsip = "";
//		
//		String sourceAreaCountry = null;
//		String sourceAreaCity = null;
//		String sourceAreaName = null;
//		String destAreaCountry = null;   
//		String destAreaCity = null;
//		String destAreaName = null;
//		String gisstr = null;
//		String gisstrBase64 = null;
//		String Sourcelon = null;
//		String Sourcelat = null;
//		String Destlon = null;
//		String Destlat = null;
//		String sourceAreaId = null;
//		String sourceAreaProvinceId = null;
//		String sourceAreaCityId = null;
//		String destAreaId = null;
//		String destAreaProvinceId = null;
//		String destAreaCityId = null;
//		String areaName_s = null;
//		String areaName_d = null;
//		
//		long upStreamOctets = 0;
//		long upStreamPacket = 0;
//		int status ;
//		
//		if(logString != null && !"".equals(logString)) {
//			String[] splits = logString.split("\\|",-1);
//			if(splits.length <= 1)
//				return;
//			String userAccount = splits[1];
//			String ipRex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";   
//			Pattern pattern = Pattern.compile(ipRex);   
//			Matcher matcher = pattern.matcher(userAccount);  
//			if(matcher.matches()) {
//				if("1".equals(splits[22]) && "6".equals(splits[7]) && splits.length >43) {//http
//				//if(splits.length ==43) {//http
//					dipL = splits[9];
//					dport = splits[10];
//					sipL = splits[12];
//					sport = splits[13];
//					domainname = splits[32];
//					url = domainname + "/" + splits[28];
//					type = splits[7]; 
//					accesstime= splits[14];
//					idcno = "446605";
//					upStreamOctets = Long.valueOf(splits[18]);
//					upStreamPacket = Long.valueOf(splits[20]);
//					status = Integer.valueOf(splits[23]);
//					userAgent = splits[26];
//					refer = splits[37];
//					cookie = splits[42];
//					if(splits.length>43) {
//						post  = splits[43];
//					}
//				}else{//非Http
//					dipL = splits[9];
//					dport = splits[10];
//					sipL = splits[12];
//					sport = splits[13];
//					domainname = "";
//					url = "";
//					type = splits[7]; 
//					accesstime= splits[14];
//					idcno = "446605";
//					upStreamOctets = Long.valueOf(splits[18]);
//					upStreamPacket = Long.valueOf(splits[20]);
//					status = Integer.valueOf(splits[23]);
//				}/*else {
//					return;
//				}*/
//				dip = CommonFunction.int2ip(Long.parseLong(dipL));
//				sip = CommonFunction.int2ip(Long.parseLong(sipL));
//				if(IPSet == null || IPSet.contains(dip) || IPSet.contains(sip)) {
//					delaytime = 0 + "";
//					long currenttime = System.currentTimeMillis();
//					if(accesstime != null && !"".equals(accesstime)) {
//						delaytime = currenttime/1000 - Long.parseLong(accesstime.trim()) + "";
//					}
//
//					//					if(_ipseg.IsLegal(idcno, dip)) {
//					if(!"".equals(dipL) && !"".equals(dport) && !"".equals(sipL) && !"".equals(sport) ) {
//						//白名单过滤
//						dsip = dip.trim() + "#" + sip.trim();
//						if(!WhiteIPSet.contains(dsip)) {
//
//							IPSearcher ipsearcher = IPSearcher.getInstance(dbJson);
//							IPDatabase SourceArea = ipsearcher.SearchIPDB(sip);
//							IPDatabase DestArea = ipsearcher.SearchIPDB(dip);
////							IPDatabase SourceArea = ipsearch.getStartIP(sip);
////							IPDatabase DestArea = ipsearch.getStartIP(dip);
//
//							if(SourceArea != null) {
//								sourceAreaCountry = SourceArea.getCountryName();
//								sourceAreaCity = SourceArea.getCityName();
//								//						sourceAreaProvince = SourceArea.getProviceName();
//								if(sourceAreaCity != null) {
//									sourceAreaName  = sourceAreaCity;
//									sourceAreaId = SourceArea.getCityID()+"";
//								} else {
//									sourceAreaName = sourceAreaCountry;
//									sourceAreaId = SourceArea.getCountryID()+"";
//								}
//								sourceAreaCityId = sourceAreaId;
//								Sourcelon = String.valueOf(SourceArea.getLon());//经度
//								Sourcelat = String.valueOf(SourceArea.getLat());//纬度
//
//								if(!"".equals(SourceArea.getProviceID()) && SourceArea.getProviceID() !=0 ) {
//									sourceAreaId = SourceArea.getProviceID()+"";
//									areaName_s = SourceArea.getProviceName().replace("省", "");
//									areaName_s = areaName_s.replace("市", "");
//								} else {
//									areaName_s = SourceArea.getCountryName();
//									sourceAreaId = SourceArea.getCountryID()+"";
//								}
//								sourceAreaProvinceId = sourceAreaId;
//							}   
//
//							if(DestArea != null ) {    
//								destAreaCountry = DestArea.getCountryName();
//								destAreaCity = DestArea.getCityName();
//								if(destAreaCity != null) {
//									destAreaName = destAreaCity;
//								} else {   
//									destAreaName = destAreaCountry;
//								}
//								Destlon = String.valueOf(DestArea.getLon());
//								Destlat = String.valueOf(DestArea.getLat());
//							}  
//
//							if(destAreaName != null && Destlon != null && Destlat != null && sourceAreaName != null && Sourcelon != null && Sourcelat != null && sourceAreaCountry != null && areaName_s != null) {
//								gisstr = destAreaName + "#" + Destlon + "," + Destlat + "#" + sourceAreaName + "#" + Sourcelon + "," + Sourcelat + "#" + sourceAreaCountry + "#" + sourceAreaId + "#" +areaName_s + "#" + sourceAreaCityId + "#" + sourceAreaProvinceId + "#" + sourceAreaCountry;
//								gisstrBase64 = new String(Base64.encodeBase64(gisstr.getBytes()));
//								collector.emit("DipStreamId",new Values(dip,dport,sip,sport,gisstrBase64,domainname,url,type,delaytime,idcno,upStreamOctets,upStreamPacket,status,userAgent,cookie,post,refer));
//								collector.ack(tuple);
//							}
//
//							//				}
//							//					}
//
//							//					if(!"".equals(dip) && !"".equals(dport) && !"".equals(sip) && !"".equals(sport) && IPSet.contains(sip)) {
//							//					if(_ipseg.IsLegal(idcno, sip)) {
//							//				if(!"".equals(dip) && !"".equals(dport) && !"".equals(sip) && !"".equals(sport)) {
//							//					IPDatabase SourceArea = ipsearch.getStartIP(sip);
//							//					IPDatabase DestArea = ipsearch.getStartIP(dip);
//
//							if(DestArea != null) {
//								destAreaCountry = DestArea.getCountryName();
//								destAreaCity = DestArea.getCityName();
//								//						sourceAreaProvince = SourceArea.getProviceName();
//								if(destAreaCity != null) {
//									destAreaName  = destAreaCity;
//									destAreaId = DestArea.getCityID()+"";
//								} else {
//									destAreaName = destAreaCountry;
//									destAreaId = DestArea.getCountryID()+"";
//								}
//								destAreaCityId = sourceAreaId;
//								Destlon = String.valueOf(DestArea.getLon());
//								Destlat = String.valueOf(DestArea.getLat());
//
//								if(!"".equals(DestArea.getProviceID()) && DestArea.getProviceID() !=0 ) {
//									destAreaId = DestArea.getProviceID()+"";
//									areaName_d = DestArea.getProviceName().replace("省", "");
//									areaName_d = areaName_d.replace("市", "");
//								} else {
//									areaName_d = DestArea.getCountryName();
//									destAreaId = DestArea.getCountryID()+"";
//								}
//								destAreaProvinceId = destAreaId;
//							}   
//
//							if(SourceArea != null) {    
//								sourceAreaCountry = SourceArea.getCountryName();
//								sourceAreaCity = SourceArea.getCityName();
//								if(sourceAreaCity != null) {
//									sourceAreaName = sourceAreaCity;
//								} else {   
//									sourceAreaName = sourceAreaCountry;
//								}
//								Sourcelon = String.valueOf(SourceArea.getLon());//经度
//								Sourcelat = String.valueOf(SourceArea.getLat());//纬度
//							}  
//
//							if(destAreaName != null && Destlon != null && Destlat != null && sourceAreaName != null && Sourcelon != null && Sourcelat != null && sourceAreaCountry != null) {
//								gisstr = destAreaName + "#" + Destlon + "," + Destlat + "#" + sourceAreaName + "#" + Sourcelon + "," + Sourcelat + "#" + destAreaCountry + "#" + destAreaId + "#" +areaName_d + "#" + destAreaCityId + "#" + destAreaProvinceId + "#" + sourceAreaCountry;
//								gisstrBase64 = new String(Base64.encodeBase64(gisstr.getBytes()));
//								collector.emit("SipStreamId",new Values(dip,dport,sip,sport,gisstrBase64,domainname,url,type,delaytime,idcno,upStreamOctets,upStreamPacket,status,userAgent,cookie,post,refer));
//								collector.ack(tuple);
//							}
//						} 
//					}
//					//					}
//					//					 **/
//				}
//			}
//		}
//		
//		
//		// TODO Auto-generated method stub
//		return null;
//	}
//
//}
